package graph

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
	"context"
	"fmt"
	"gql3/db"
	"gql3/graph/customize/apierror"
	"gql3/graph/generated"
	"gql3/graph/model"
	"strconv"
	"time"

	"gorm.io/gorm"
)

func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
	fmt.Println("create todo")
	todo := &model.Todo{
		Text:   input.Text,
		Done:   false,
		UserId: input.UserID,
	}
	db.DB.Model(&model.Todo{}).Create(todo)
	return todo, nil
}

func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) (*model.User, error) {
	fmt.Println("create user")
	var user *model.User
	var err error
	err = db.DB.Transaction(func(tx *gorm.DB) error {
		info := &model.UserExtraInfo{

			Address: input.Address,
		}
		tx.Create(info)
		user = &model.User{
			CreatedAt:       time.Time{},
			UpdatedAt:       time.Time{},
			Name:            input.Name,
			UserExtraInfoId: info.ID,
		}
		tx.Create(user)
		return nil
	})
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (r *queryResolver) Todos(ctx context.Context) ([]*model.Todo, error) {
	fmt.Println("queryResolver: Todos")
	todos := make([]*model.Todo, 0)
	db.DB.Model(&model.Todo{}).Find(&todos)
	return todos, apierror.UserNotExist{}
}

func (r *todoResolver) User(ctx context.Context, obj *model.Todo) (*model.User, error) {
	fmt.Println("todoResolver: user")
	userId, _ := strconv.ParseInt(obj.UserId, 10, 64)
	return r.For(ctx).UserById.Load(userId)
}

func (r *userResolver) UserExtraInfo(ctx context.Context, obj *model.User) (*model.UserExtraInfo, error) {
	fmt.Println("userResolver: UserExtraInfo")
	return r.For(ctx).UserExtraInfoById.Load(obj.UserExtraInfoId)
}

// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }

// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }

// Todo returns generated.TodoResolver implementation.
func (r *Resolver) Todo() generated.TodoResolver { return &todoResolver{r} }

// User returns generated.UserResolver implementation.
func (r *Resolver) User() generated.UserResolver { return &userResolver{r} }

type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type todoResolver struct{ *Resolver }
type userResolver struct{ *Resolver }

// !!! WARNING !!!
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
// one last chance to move it out of harms way if you want. There are two reasons this happens:
//  - When renaming or deleting a resolver the old code will be put in here. You can safely delete
//    it when you're done.
//  - You have helper methods in this file. Move them out to keep these resolver files clean.
func (r *todoResolver) ID(ctx context.Context, obj *model.Todo) (string, error) {
	fmt.Println("create todo")

	panic(fmt.Errorf("not implemented"))
}
func (r *userResolver) ID(ctx context.Context, obj *model.User) (string, error) {
	panic(fmt.Errorf("not implemented"))
}
